/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import xml.entity.Account;
import xml.util.DBConnection;

/**
 *
 * @author Kyros
 */
public class AccountDAO {
    
    DBConnection conInfo;
    
    public void insertAccount(Account account) throws SQLException, Exception {
        Connection conn = null;
        Statement stm = null;
        conInfo = new DBConnection();

        try {
            Class.forName(conInfo.getDriverName());
            conn = DriverManager.getConnection(conInfo.getURL(), conInfo.getUsername(), conInfo.getPassword());
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.append("INSERT INTO account (username, password, type)"
                    + " VALUES ('")
                        .append(account.getUserName()).append("', '")
                        .append(account.getPassword()).append("', '")
                        .append(account.getType()).append("')");

            stm = conn.createStatement();
            stm.executeUpdate(strBuilder.toString());

            stm.close();
            conn.close();
        } catch(SQLException e) {
            throw(e);
        } catch(Exception e) {
            throw(e);
        } finally {
            try {
                stm.close();
            } catch (SQLException e) {
                throw(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                throw(e);
            }
        }
    }

    public List<Account> getAllAccount() throws SQLException, Exception {
        List<Account> accountList = new ArrayList<Account>();
        ResultSet rs = null;
        Connection conn = null;
        Statement stm = null;
        StringBuilder strBuilder = new StringBuilder();
        conInfo = new DBConnection();
        Account account;

        try {
            Class.forName(conInfo.getDriverName());
            conn = DriverManager.getConnection(conInfo.getURL(), conInfo.getUsername(), conInfo.getPassword());
            strBuilder.append("SELECT * FROM account");
//            strBuilder.append("(select username as userName, password, firstName + ' ' + midName + ' ' + lastName as fullName,	'staff' as userRole from Staff) union");
//            strBuilder.append("(select username as userName, password, firstName + ' ' + midName + ' ' + lastName as fullName, 'student' as userRole from Student) union");
//            strBuilder.append("(select username as userName, password, firstName + ' ' + midName + ' ' + lastName as fullName, 'teacher' as userRole from Teacher)");
//            if (condition != null && !condition.equals("")) {
//                strBuilder.append(condition);
//            }
            stm = conn.createStatement();
            rs = stm.executeQuery(strBuilder.toString());
            accountList = new ArrayList<Account>();
            while (rs.next()) {
                account = new Account(rs.getString("username"), rs.getString("password"), Integer.parseInt(rs.getString("type")));
                accountList.add(account);
            }
            return accountList;
        } catch (SQLException e) {
            throw (e);
        } catch (Exception e) {
            throw (e);
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                throw (e);
            }
            try {
                stm.close();
            } catch (SQLException e) {
                throw (e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                throw (e);
            }
        }
    }
}
